<?php
namespace Home\Controller;
header("Content-type: text/html; charset=utf-8");
use Common\Controller\HomebaseController;
class RegisterController extends HomebaseController {


    public function index(){
        $this->display("login");
    }
    public function register(){
        $region = M("region");

        $arr = $region
            ->where("parent_id = 100000")
            ->field("name,id")
            ->select();
//        var_dump($arr);
//        die;
        $this->assign("pros",$arr);
        $this->display();
    }


    public function citys(){

        $pid = $_GET['id'];


        $region = M("region");
        $arr = $region
            ->where("parent_id = $pid")
            ->field("name,id")
            ->select();
//        把数组转换成json返回到页面
        $list = json_encode($arr);
        echo ($list);
    }

    public function towns(){

        $pid = $_GET['id'];


        $region = M("region");
        $arr = $region
            ->where("parent_id = $pid")
            ->field("name,id")
            ->select();
//        把数组转换成json返回到页面
        $list = json_encode($arr);
        echo ($list);
    }


    public function doReg(){

    /**
     *
        //原生php上传图片方法
        $data = $_FILES['file'];
        var_dump($data);
        $target_path = "uploads/";

        $target_path = $target_path .$_FILES['file']['name'];
        var_dump($target_path);
        $m = move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
        if($m) {
            echo " 上传成功";
        } else{
            echo "上传失败";
        }
     *
     */

        /* tp上传图片的方法 */


        // 实例化上传类【使用CMF核心里面的\simplewind\Core\Library\Think\Upload.class.php】
            $upload = new \Think\Upload();


        // 设置附件上传大小
            $upload->maxSize   =     3145728 ;


        // 设置附件上传类型
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');


        // 设置附件上传目录
            $upload->savePath  =      './User/';



        // 上传单个文件
            $info   =   $upload->uploadOne($_FILES['file']);

            if(!$info) {
            // 上传错误提示错误信息
                $this->error($upload->getError());
            }else{
            // 上传成功 获取上传文件信息
            // echo $info['savepath'].$info['savename'];

                $data['photo']=$info['savepath'].$info['savename'];

            // $this->success("文件上传成功");
            // $this->success('操作完成',U("Register/register"),6);
            }




        $data['account']=$_POST['account'];
        $data['pass']=$_POST['pass'];
        $data['sex']=$_POST['sex'];
        $data['tel']=$_POST['tel'];
        $data['pro_id']=$_POST['pro'];
        $data['city_id']=$_POST['city'];
        $data['town_id']=$_POST['town'];
        $data['province']=$_POST['pro_name'];
        $data['city']=$_POST['city_name'];
        $data['town']=$_POST['town_name'];
        $data['sites']=$_POST['site'];
        $data['time']=date("Y-m-d H:i:s",time());
        $members = M("members");
		$where['account']=$data['account'];
        $n = $members->where($where)->find();
        if(!$n){
            $members->add($data);
            $this->success("注册完成",U("Home/register/index"),3);
        }else{
            $this->error("账户已注册！");
        }

    }


//发送验证码
    public function yzma(){

//        生成随机验证码
        $arr=range(0,9);
        $narr = array_rand($arr,4);
//        print_r($narr);
        $verify = "";
        foreach($narr as $values)
        {
            $verify .= $values;
        }
//        echo $verify;


        vendor('Ucpaas.Ucpaas','','.class.php');



        /**
         * 发送模板短信
         * @param to 手机号码集合,用英文逗号分开
         * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
         * @param $tempId 模板Id
         */
        function sendTemplateSMS($to,$datas,$tempId)
        {
            // 初始化REST SDK
            global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;

            //主帐号
            $accountSid= '8a216da859a508850159a62400970296';

//主帐号Token
            $accountToken= 'dc2ec609eb234cfe99bc91c2bac1d0f0';

//应用Id
            $appId='8a216da859a508850159a6240103029c';

//请求地址，格式如下，不需要写https://
            $serverIP='app.cloopen.com';

//请求端口
            $serverPort='8883';

//REST版本号
            $softVersion='2013-12-26';


            $rest = new \REST($serverIP,$serverPort,$softVersion);
            $rest->setAccount($accountSid,$accountToken);
            $rest->setAppId($appId);


            // 发送模板短信
            echo "Sending TemplateSMS to $to <br/>";
            $result = $rest->sendTemplateSMS($to,$datas,$tempId);
            if($result == NULL ) {
                echo "result error!";

            }
            if($result->statusCode!=0) {
                echo "error code :" . $result->statusCode . "<br>";
                echo "error msg :" . $result->statusMsg . "<br>";
                //TODO 添加错误处理逻辑
            }else{
                echo "Sendind TemplateSMS success!<br/>";
                // 获取返回信息
                $smsmessage = $result->TemplateSMS;
                echo "dateCreated:".$smsmessage->dateCreated."<br/>";
                echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
                //TODO 添加成功处理逻辑
            }
        }

//Demo调用,参数填入正确后，放开注释可以调用
        //获取传递过来的手机号
        $tel = $_POST['tel'];
        $data=array($verify,5);  // 第一个是验证码，第二个是过时时间
        $tempId = 1;    //这个是申请的项目的ID

        //在设置cookie前先把之前的设置的焚毁
        setcookie("ma",'',0);
        setcookie("tel",'',0);
        //设置cookie5分钟过时

//        md5加密，防止在控制器里发现验证码
        setcookie("ma",$verify, time()+60*5);
        setcookie("tel",$tel, time()+60*5);

//        echo ($_COOKIE['ma']);
//        echo ($_COOKIE['tel']);

        sendTemplateSMS($tel,$data,$tempId);

    }

//查看验证码与cookie存的值是否一致
    public function yzcookie(){
        $ma = $_COOKIE['ma'];
        $tel = $_COOKIE['tel'];

//        md5加密，以便与原来的加密过的验证码获得统一
        $ma1 = $_POST['ma'];
        $tel1 = $_POST['tel'];

        if($tel == $tel1){
//            echo "手机验证成功";
            if($ma == $ma1){
//            echo "验证码正确";
                echo "100100";
            }else{
//            echo "验证码错误";
                echo "100102";
            }
        }else{
//            echo "手机验证失败";
            echo "100101";
        }




    }


//    登录的验证码
    public function loginVerify(){
        $config =    array(
            'expire'       =>   10,     //有效期
            'fontSize'     =>    30,    // 验证码字体大小
             'length'      =>    4,     // 验证码位数
             'useNoise'    =>    false, // 关闭验证码杂点
             'useCurve'    =>    false, // 关闭混淆曲线
    );
        $Verify =   new \Think\Verify($config);
        $Verify->entry();
    }


//    登录
    public function doLogin(){

        $user = $_POST['username'];
        $pass = $_POST['password'];
        $code2 = $_POST['verify'];


// 检测输入的验证码是否正确，$code为用户输入的验证码字符串
        function check_verify($code, $id = ''){
            $verify = new \Think\Verify();
            return $verify->check($code, $id);
        }

        $true1 = check_verify($code2, $id = '');

//        验证码不正确返回 1
//        if(!$true1){
//            echo "1";
//        }

//        检测账户是否存在
        $members = M("members");
        $where['account'] = $user;
        $n = $members->where($where)->find();

//        $sql = $members->getLastSql();
//       echo "$sql";

//        如果账户名不存在 返回2
//            if(!$n){
//                echo "2";
//            }


        $m = $members->where($where)->field("pass")->find();

//        $sql = $members->getLastSql();
//       echo "$sql";
//        var_dump($m);
//        die;


        if(!$true1){
//            验证码不正确返回 1
            echo "1";
        }else{
            if(!$n){
//                账户名不存在
                echo "2";
            }else{
                if($m['pass'] != $pass){
                    echo $m['pass']."?";
                    echo $pass;
//                密码错误
                    echo "3";

                }else{
                    echo "4";
//                    验证成功，储存session
                    session('name',$user);
//                    根据name获取其它信息，并存入session，方便页面使用
                    $infor = $members->where($where)->field("photo")->find();
                    session('photo',$infor['photo']);
                }

            }
        }
    }


//    退出登录
    public function loginOut(){

        session("name",null);
        $this->redirect("Home/index/index");
    }
}